package main

import "fmt"

// map的key是字符串，value是函数类型
var operationMap = make(map[string]func(x, y int) int)

func init() {
	//将函数赋值给变量
	operationMap["+"] = add
	operationMap["-"] = minus
	operationMap["*"] = multiply
	operationMap["/"] = divide
	operationMap["%"] = mod
}

func add(x, y int) int {
	return x + y
}

func minus(x, y int) int {
	return x - y
}

func multiply(x, y int) int {
	return x * y
}

func divide(x, y int) int {
	if y == 0 {
		return 0
	}
	return x / y
}

func mod(x, y int) int {
	if y == 0 {
		return 0
	}
	return x % y
}

// 从map中获取函数，获取成功则调用函数
func operate(operator string, x, y int) int {
	if f, ok := operationMap[operator]; ok {
		return f(x, y)
	}
	return 0
}

func main() {
	result := operate("+", 10, 20)
	fmt.Println(result)
}
